package com.java.base.exercise.threadpool;

import org.junit.Test;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author RenPu
 * @Date 2021/7/3 15:37
 * @Version 1.0
 * @Description:线程池的策略：1：终止模式， 2：抛弃策略，
 * 3：抛弃旧任务策略，
 * 4：调用者运行的策略
 * 任务sumbit和exceture执行的区别
 **/
public class ThreadPoolStrategy {


    /**
     * submit方法案例的编写
     *
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @Test
    public void cachePool() throws ExecutionException, InterruptedException {
        ExecutorService threadPool = Executors.newCachedThreadPool();
        Integer integer = threadPool.submit(() -> {
            Thread.sleep(10 * 10);
            return 2 * 5;
        }).get();
        System.out.println("执行的结果值为：" + integer);


    }


    /**
     * execute方法案例的编写
     *
     * @throws InterruptedException
     */
    @Test
    public void fxiedPool() throws InterruptedException {
        ExecutorService threadPool = Executors.newCachedThreadPool();

        threadPool.execute(() -> {
            try {
                Thread.sleep(10 * 10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Integer integer = 2 * 5;
            System.out.println("execute方法执行的结果为：" + integer);
        });

        Thread.sleep(20 * 10);
    }


}
